/**
 * 
 */
package com.liusoft.android.fmagic;

import com.liusoft.android.utils.LogUtils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * 图像选择器
 * @author Winter Lau
 */
public class ImageSelector extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.gallery);
		
		GridView gview = (GridView)this.findViewById(R.id.gallery);		
		gview.setAdapter(new MyGridView.ImageAdapter(this));
		gview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ImageSelector.this.setResult(GameGlobal.pics[position]);
				ImageSelector.this.finish();
			}});
	}
	
	/**
	 * 可选择的图片展示
	 * @author Winter Lau
	 */
	static class MyGridView extends GridView {

		public MyGridView(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
		}

		public MyGridView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		public MyGridView(Context context) {
			super(context);
		}
		
		static class ImageAdapter extends BaseAdapter {
			
			Context mContext;
			
			public ImageAdapter(Context ctx) {
				this.mContext = ctx;
			}
			
			@Override
			public int getCount() {
				LogUtils.debug(this.getClass().getName(), "getCount = %d", GameGlobal.pics.length);
				return GameGlobal.pics.length;
			}

			@Override
			public Object getItem(int position) {
				return position;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ImageView iv = new ImageView(mContext);
				iv.setImageResource(GameGlobal.pics[position]);
				iv.setScaleType(ImageView.ScaleType.FIT_XY);
				iv.setAdjustViewBounds(true);
				iv.setMaxHeight(130);
				iv.setMaxWidth(130);				
				return iv;
			}
			
		}
	}
	
	

}
